/*
 * 作者: 黄伟
 * 时间: 2017-01-19
 * 说明：
 * Copyright (c) 2016-2017 中研沃兮
 */
define(function(require) {

	require("jquery");
	var appInfo = require('app/AppInfo');
	var c = require('app/utils/CommonUtils');
	var hc = require('app/utils/HttpClientUtils');
	var tip = require('app/utils/TipUtils');
	var EchoLazyUtils = require('app/utils/EchoLazyUtils');
	var RoomInfoUtils = require('app/utils/RoomInfoUtils');
	var UserInfoUtils = require('app/utils/UserInfoUtils');
	var CacheUtils = require('app/utils/CacheUtils');
	var HashMap = require('app/utils/HashMapUtils');
	var APIUtils = require('app/utils/APIUtils');
	var action = {};
	var win = window;
	//var param = api.pageParam;

	EchoLazyUtils.load();
	win.roomMap = new HashMap();

	action.showScrollPicture = function(){
		var UIScrollPicture = api.require('UIScrollPicture');
		UIScrollPicture.open({
		    rect: {
		        x: 0,
		        y: $("#header-bar").height(),
		        w: APIUtils.winW(),
		        h: $("#scroll_pic").height()
		    },
		    data: {
		        paths: [
		            'widget://image/banner2.png', //高考专业
		            'widget://image/banner3.png',  //健康教育
		            'widget://image/banner5.png',  //上科大原单晚会
		            'widget://image/banner4.png'  //轨道交通
		        ]

		    },
		    styles: {
		        indicator: {
		            align: 'center',
		            color: '#FFFFFF',
		            activeColor: '#DA70D6'
		        }
		    },
		    placeholderImg: 'widget://res/imgplaceholder.png',
		    contentMode: 'scaleToFill',
		    interval: 3,
		    fixedOn: api.frameName,
		    loop: true,
		    fixed: false
		}, function(ret, err) {
		    if (ret) {
		    	if(ret.eventType == "click"){
		    		var index = ret.index;
		    		if(index == 1){

		    		}else if(index ==2){

		    		}else if(index ==3){

		    		}
		    	}
//		        alert(JSON.stringify(ret));
		    } else {
//		        alert(JSON.stringify(err));
		    }
		});

	}
	action.showHotVideo = function(){

	}
	action.showRecentVideo = function(){

	}

	action.renderRooms = function($container, rooms){

		$container.html("");

		rooms.forEach(function(item) {
			win.roomMap.put(item.uuid,item);
			var status_div = "";
			var live_cover_url = item.livecoverurl;
			var cache_user_cover_url = "";

			var default_live_cover_url =  "../image/room_cover_default.png";

			var w = parseInt((APIUtils.winW() - parseInt(c.remToPx(0.625 * 2 + 0.125*2))) / 2);
			var h = parseInt(w * 0.5625);

			if (c.isNullStr(live_cover_url) || live_cover_url == "null") {
				live_cover_url = default_live_cover_url;
			} else {
				live_cover_url = c.handleUrl(live_cover_url);
				//网络地址，缓存图片
				cache_user_cover_url = CacheUtils.imageCache(live_cover_url);
			}

			if(live_cover_url == default_live_cover_url){
				var live_cover_div = $('<img src="'+default_live_cover_url+'" style="width:'+w+'px; height:'+h+'px">');
			}else{
				if(cache_user_cover_url == live_cover_url){
					var live_cover_div = $('<img data-echo="'+live_cover_url+'" src="../image/imgplaceholder.png" style="width:'+w+'px; height:'+h+'px">');
				}else{
					var live_cover_div = $('<img src="'+cache_user_cover_url+'" style="width:'+w+'px; height:'+h+'px">');
				}
			}

			var live_type_div = '';
			if (item.liveType == "1") { // 视频 直播
				live_type_div　=
					'<div class="room-type-icon" style="background: brown;">'+
						'视频直播'+
					'</div>';
			} else if (item.liveType == "2") { // PPT+语音 直播
				live_type_div　 =
					'<div class="room-type-icon" style="background: brown; ">' +
					'PPT+语音' +
					'</div>';
			} else if (item.liveType == "3") { // 外站其他直播或视频，但不现实PPT+语音字样, 视频
				live_type_div　 =
					'<div  class="room-type-icon"  style="background: brown; ">' +
					'视频' +
					'</div>';
			}

			if (item.type == "3") {
				status_div =
					'<div class="room-status-icon" style="background: rgb(237,116,55);">' +
					'回顾' +
					'</div>';
			} else if (item.type == "1") {
				var now = new Date().getTime();
				if (now < item.time) {
					status_div =
						'<div class="room-status-icon" style="background: rgb(95,174,231);">' +
						'预告' +
						'</div>';
				} else {

					if (now < item.endtime) {
						status_div =
							'<div class="room-status-icon" style="background: cornflowerblue;">' +
							'直播' +
							'</div>';
					} else {
						status_div =
							'<div class="room-status-icon" style="background: rgb(237,116,55);">' +
							'回顾' +
							'</div>';
					}
				}
			}
			var pwddiv = '<i class="aui-iconfont aui-icon-lock" style="color:#609700;font-weight:800"></i> &nbsp;';
			if(c.isNullStr(item.pwd) || item.pwd == false){
				pwddiv = "";
			}
			var div = $(
				'<div class="aui-col-xs-6 room-item-wrap" data-uuid="'+item.uuid+'">'+
                    '<div>'+
                    	live_cover_div.prop('outerHTML') +
                		status_div +
                		live_type_div +
                    '</div>'+
                    '<div class="room-title aui-ellipsis-2">'+pwddiv+ item.name+'</div>'+
                    '<div class="room-starttime">'+c.timeFormat(item.time)+'</div>'+
                '</div>'
				);
			$container.append(div);
		});

		EchoLazyUtils.load();
		api.parseTapmode();

	}
	action.fetchInHeaderMode = function(){



		var that = this;

		var url = appInfo.serverbase + "/luyan/homepage";
		var body = {
		};

		hc.post(url, body, function(resp) {
			if (resp.status != "200") {
				api.refreshHeaderLoadDone();
				return;
			}
			api.refreshHeaderLoadDone();
			var data = resp.result;


			action.renderRooms($("#hot-rooms-container"),data.hot_rooms);
			action.renderRooms($("#new-rooms-container"),data.new_rooms);

			$(".room-item-wrap").click(function(){

				var room = win.roomMap.get($(this).data("uuid"));
					room.roomuuid=$(this).data("uuid");
	//			room.avatar_use_cache =$(this).data("avatar_use_cache");
	//			room.cover_use_cache = $(this).data("cover_use_cache");
				c.debug("click room:",room);
				if(!UserInfoUtils.isLogined()){
				//未登陆则打开LoginWin.js
					if(api.systemType == "ios"){
						if(APIUtils.doesWXloginDisplay()){
							api.openWin({
						        name: 'LoginAndRegistWin',
						        url: './LoginAndRegistWin.html'
					    });
					    return;
						}else{
							api.openWin({
					            name: 'RoomWin',
					            url: './RoomWin.html',
					            pageParam: room
				      });
						}
					}else{
						api.openWin({
									name: 'LoginAndRegistWin',
									url: './LoginAndRegistWin.html'
						});
						return;
					}
				}else{

				//进入RoomWIn页面, 并传入参数room
					api.openWin({
			            name: 'RoomWin',
			            url: './RoomWin.html',
			            pageParam: room
		            });
				}
			});

		});

	}

	return action;
});
